home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / winfsr21.zip / WINFSR.TXT < prev   
Text File  |  1992-02-18  |  8KB  |  145 lines

  1.  
  2.             WinFSR 2.1    by Fran Finnegan (76244,145)
  3.   Copyright (c) 1991-92 Finnegan O'Malley & Company Inc.  All Rights Reserved.
  4.            First Published in PC Magazine, November 12, 1991.
  5.  
  6. -------------------------------------------------------------------------------
  7.  
  8. MONITORING FREE SYSTEM RESOURCES:  Use WINFSR.EXE to keep a constant eye on
  9. your Free System Resources in Windows 3.x.
  10.  
  11. -------------------------------------------------------------------------------
  12.  
  13. MONITORING FREE SYSTEM RESOURCES
  14.  
  15. If you execute the Help About command in the Program Manager or File Manager
  16. (or the File About command in the MS-DOS Executive), Windows pops up a dialog
  17. box that displays the amount of Free System Resources (FSR) available to your
  18. system.  You may ask:  What are Free System Resources and why should I care?
  19.  
  20. FSR are really nothing.  Literally.  They are not resource objects lurking
  21. within Windows waiting to be used, nor are they a reference to the System
  22. Resources described on page 494 of the Windows User's Guide.  Free System
  23. Resources is just a fancy name for the amount of free local memory, expressed
  24. as a percentage, that's available to two key Windows components:  the Graphics
  25. Device Interface (GDI.EXE) and the User Interface (USER.EXE) These two modules
  26. plus the Kernel module (KERNEL.EXE, KRNL286.EXE or KRNL386.EXE, depending on
  27. whether you're in Real, Standard or 386 Enhanced mode) comprise the Dynamic
  28. Link Libraries (DLLs) that are the heart and soul of Windows.
  29.  
  30. But why should you care?  Simple:  without FSR you can not run Windows
  31. applications.  As you do run Windows applications, they use up available FSR.
  32. If you run a number of big Windows applications (such as Excel and Word for
  33. Windows) at the same time, you may run out of FSR and lock up your machine.  No
  34. matter how much free system or global memory you have (historically the
  35. constraint faced by users), I guarantee you that someday you'll run out of FSR
  36. as you run more and more applications concurrently.
  37.  
  38. The management of the FSR rests with the USER and GDI modules mentioned above.
  39. These DLLs each contain multiple executable-code segments and a single default
  40. data segment (so-called local memory of the DLL).  The Intel 80x86 architecture
  41. constrains these segments to a maximum size of 64K bytes.  The single default
  42. data segments of GDI and USER are used by Windows for performance reasons:
  43. Accessing objects in these data segments with "near" pointers is relatively
  44. fast.  Since Windows stores many objects (such as windows, dialog boxes,
  45. controls, icons, cursors, bitmaps, menus, pens, and brushes) in the data
  46. segments of GDI and USER, the 64K local-memory limit becomes a significant and
  47. easy-to- reach constraint if you run a lot of Windows applications
  48. concurrently.
  49.  
  50. So instead of a DOS 640K barrier, we now have a Windows 64K barrier.  It's
  51. disheartening to think that a 486 machine with 16MB of real global memory
  52. running in 386 Enhanced mode can not run one copy of Word for Windows and five
  53. copies of Excel without hitting the wall imposed by compatibility with the
  54. over- ten-year-old 8086/8088 chip!  The fifth or sixth copy of Excel will
  55. probably lock up your machine, forcing a cold re-boot.    You would lose anything
  56. you hadn't saved, and without constantly remembering to check the FSR, you
  57. wouldn't even see it coming!  It might be helpful to know how much FSR your
  58. applications use.  You can experiment by running Program Manager's Help About
  59. command before and after you launch an application.  If you start up Windows
  60. with nothing but Program Manager running, you will see that your FSR percentage
  61. is about 75-80%.  (The actual number may be greater, depending on how many
  62. icons you have displayed in Program Manager; each of these icons takes up some
  63. FSR.) You'll find that running Word for Windows consumes 8% of FSR.  The first
  64. copy of Excel requires 18% of FSR while subsequent invocations use up 12%.
  65.  
  66. Experimenting with your applications to see which ones hog FSR may be helpful.
  67. But the solution to this problem is for Microsoft to move some of the objects
  68. stored within the 64K default data segments of GDI and USER out into global
  69. memory.  For example, in Windows 3.1 they have moved menus out of USER's data
  70. segment and stored them in global memory.  The FSR problem is still with us in
  71. Windows 3.1, but to a lesser degree.
  72.  
  73. Since the FSR constraint will be with us for a while, we might as well be able
  74. to monitor it.    Unfortunately, Microsoft does not document how the FSR
  75. percentage is calculated.  As such, it's taken independent Windows applications
  76. developers about a year to figure it out.
  77.  
  78. The key to calculating FSR lies in an undocumented Windows 3.0 call:
  79.  
  80.     extern DWORD FAR PASCAL GetHeapSpaces(HANDLE hModule);
  81.  
  82. This function is exported by the Kernel modules as ordinal 138.  (Because it's
  83. undocumented, application developers would have to explicitly import this
  84. function in their .DEF files.) It accepts a handle to a module, the sort of
  85. which you would get with the call:
  86.  
  87.     GetModuleHandle("USER");
  88.  
  89. GetHeapSpaces returns a DWORD in which the low word is the number of bytes of
  90. free local heap space and the high word is the number of bytes of maximum local
  91. heap space.  (Both values take into account local heaps that have not been
  92. physically expanded to the 64K limit.) Dividing the low word by the high word
  93. and multiplying the result by 100 yields the percentage of free local memory
  94. for the requested module.  Since the FSR value used is the minimum of the GDI
  95. and USER values, this calculation must be performed twice.
  96.  
  97. Armed with this knowledge, I've written a small utility named WINFSR, which
  98. allows you to watch your FSR without having to open the About boxes.  WINFSR
  99. constantly monitors the FSR value and displays the percentage in an icon.  You
  100. can also display a larger dialog box containing the FSR value, as well as the
  101. USER and GDI values.  WINFSR.EXE and WINFSR.ZIP (which contains the source code
  102. shown in Figure 1) can be downloaded from PC MagNet.
  103.  
  104. The logic of WINFSR is pretty straightforward.    It's made up of only three C
  105. functions.  An iconic window and a dialog box are created to display the
  106. results of the FSR calculation.  The calculation is then performed once each
  107. second (on a WM_TIMER message), and the displays are updated if the FSR's
  108. components change.  If you want to just keep an eye on the FSR percentage, you
  109. can select the OK button on the dialog box to hide it; only the stand-alone
  110. icon will remain on-screen.  To unhide the box, click on the icon to bring up
  111. the System menu and choose Restore or Maximize.
  112.  
  113. Since Windows or applications or both will most-likely crash if FSR gets too
  114. close to 0% , the dialog box serves as a warning mechanism.  By default, the
  115. dialog box will re-appear if FSR gets below 20%.  At this point, you should
  116. probably save any open files or close some applications.  If you would rather
  117. be warned at a different percentage, say 10%, you should add the following
  118. section to your WIN.INI file:
  119.  
  120.     [WinFSR]
  121.     %=10
  122.  
  123. The %=x line determines the trigger point at which the dialog box will be re-
  124. displayed.
  125.  
  126. Watching the dialog box can be instructional to Windows application developers.
  127. For example, they can use it to see if they leave any undeleted objects in GDI
  128. by comparing the GDI usage values from before and after their applications run.
  129.  
  130. Windows application developers should note that Windows 3.1 will have a
  131. documented function to get the FSR number, although its name hasn't been
  132. finalized at the time I'm writing this. WINFSR will have be updated to use
  133. the proper functions depending on your version of Windows.
  134.  
  135. -------------------------------------------------------------------------------
  136.  
  137. OPENING WINDOWS
  138.  
  139. Share your Windows experiences with Fran Finnegan; we'll pay you $50 for any
  140. tip we print.  Mail your contribution on a disk, with a printout, to Windows,
  141. PC Magazine, One Park Avenue, New York, NY 10016, or e-mail it (re: PC
  142. Magazine) to:  PC MagNet:  76244,145; MCI Mail:  Fran Finnegan; BIX:
  143. franfinnegan; GEnie:  finnegan; Internet:  finnegan@well.sf.ca.us.
  144.  
  145.